package com.pany.service;

import java.util.List;

import javax.persistence.Query;

import org.apache.commons.collections.CollectionUtils;

import com.pany.domain.IConstant.ITranResultCode;
import com.pany.domain.models.VIPInfo;
import com.pany.exceptions.ServiceException;

import play.db.jpa.JPABase;

public class VIPInfoService
{
    public static VIPInfo getVipBalanceByUname(String uname)
    {
        Query q = JPABase.em().createQuery(" select vipId,balance from VIPInfo where vipName=?");
        q.setParameter(1, uname);
        List<Object[]> list = q.getResultList();
        if (CollectionUtils.isEmpty(list))
        {
            return null;
        }
        VIPInfo info = new VIPInfo();
        info.setVipId(String.valueOf(list.get(0)[0]));
        info.setBalance((Double)list.get(0)[1]);
        return info;
    }
    
    public static VIPInfo vipLogin(VIPInfo vipInfo)
    {
        VIPInfo tranDB = null;
        tranDB = VIPInfo.find("from VIPInfo where vipName = ? AND passwd = ?",
            vipInfo.getVipName(),
            vipInfo.getPasswd()).first();
        if (null == tranDB)
        {
            throw new ServiceException(ITranResultCode.TRAN_MEMBER_PASSWD_UNCORRECT);
        }
        return tranDB;
    }
}
